package com.example.shop_addres.controller;

import com.example.shop_addres.entity.Members;
import com.example.shop_addres.service.MembersService;
import com.example.shop_addres.utils.JsonResult;
import com.example.shop_addres.utils.Tool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("members")
public class MembersController {
    @Autowired
    private MembersService membersService;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @Autowired
    private Members members;
    @PostMapping("register")
    public JsonResult register(@RequestParam("mobile") String mobile,
                               @RequestParam("name") String name,
                               @RequestParam("detailedAddress") String detailedAddress) {
        //重复性验证
        Members membersInfo = membersService.getMembersByMobile(mobile);
        if (membersInfo != null) {
            return JsonResult.failed("该手机号码已经使用，请更换.");
        }
        return JsonResult.success("注册成功", null);
    }

//    //获取短信验证码
//    @PostMapping("get_sms_code")
//    public JsonResult getSMSCode(@RequestParam("mobile") String mobile) {
//        //验证手机号格式
//        if(mobile==null || mobile.trim().length() == 0 || !Tool.isMobile(mobile)){
//            return JsonResult.failed("请输入正确格式的手机号码");
//        }
//        //生成4位数字随机验证码
//        long randomNum = Tool.generateRandomNumber(4);
//        //存储到redis
//        stringRedisTemplate.opsForValue().set(mobile,String.valueOf(randomNum),60*60, TimeUnit.SECONDS);
//        return JsonResult.success("获取成功", randomNum);
//    }
//    @PostMapping("login")
//    public JsonResult login(@RequestParam("mobile") String mobile, @RequestParam("code") String code) {
//        //短信验证码验证
//        String smsCode = stringRedisTemplate.opsForValue().get(mobile);
//        if (smsCode == null || !smsCode.equals(code)) {
//            return JsonResult.failed("验证码输入错误.");
//        }
//        Members memberInfo = membersService.getMembersByMobile(mobile);
//        if (memberInfo == null) {
//            return JsonResult.failed("登录失败");
//        }
//
//        //删除缓存验证码
//        stringRedisTemplate.delete(mobile);
//        HashMap<String, Object> mapMemberInfo = new HashMap<>();
//        mapMemberInfo.put("id", memberInfo.getId());
//        mapMemberInfo.put("mobile", memberInfo.getMobile());
//        return JsonResult.success("登录成功",mapMemberInfo);
//    }


    @PostMapping("update")
    public JsonResult update(@RequestParam("id") String id,
                             @RequestParam("detailedAddress") String detailedAddress
                             ) {
        Members memberInfo = membersService.getMembersByMobile(id);
        if (memberInfo == null) {
            return JsonResult.failed("登录失败");
        }
        int result = membersService.update(id, detailedAddress);
        if (result == 0) {
            return JsonResult.failed("设置成功");
        }
        return JsonResult.success("设置成功",null);
    }

    @PostMapping("delete")
    public JsonResult delete(@RequestParam("mobile") String mobile
    ) {
        Members memberInfo = membersService.getMembersByMobile(mobile);
        if (memberInfo == null) {
            return JsonResult.failed("删除失败");
        }
        int result = membersService.delete(mobile);
        if (result == 0) {
            return JsonResult.failed("删除成功");
        }
        return JsonResult.success("删除成功",null);
    }
}
